package mylab.starters.oss.config.fastdfs;

import com.github.tobato.fastdfs.FdfsClientConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.RequiredArgsConstructor;
import mylab.starters.oss.OssRule;
import mylab.starters.oss.config.OssAutoConfiguration;
import mylab.starters.oss.config.OssProperties;
import mylab.starters.oss.support.fastdfs.FastdfsTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@AutoConfigureAfter({OssAutoConfiguration.class, FdfsClientConfig.class})
@ConditionalOnProperty(value = FastdfsAutoConfiguration.PREFIX, havingValue = "true")
public class FastdfsAutoConfiguration {

    static final public String PREFIX = OssProperties.PREFIX + ".fastdfs.enabled";

    final private OssProperties ossProperties;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean({FastFileStorageClient.class, OssRule.class})
    public FastdfsTemplate fastdfsTemplate(FastFileStorageClient client, OssRule ossRule) {
        return new FastdfsTemplate(ossProperties.getFastdfs(), ossRule, client);
    }
}
